package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.UserVipGrowthLogDao;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserVipGrowthLog;
import com.qdlc.p2p.dal.model.UserVipGrowthLogModel;
/**
 * VIP成长值 dao实现
 * @author zlhx
 * @version 2.0
 * @date 2015-05-16 10:34:00
 *
 */
@Repository("userVipGrowthLogDao")
public class UserVipGrowthLogDaoImpl extends BaseDaoImpl<UserVipGrowthLog> implements
		UserVipGrowthLogDao {
	@Override
	public double getGrowthTotalByTender(User user,Date date) {
		String sql = "select sum(growth_value) from " + Global.DB_PREFIX + "user_vip_growth_log where  date_format(add_time,'%Y-%m-%d')=? and user_id=?";
		Query query = em.createNativeQuery(sql)
				.setParameter(1, DateUtil.dateStr2(date))
				.setParameter(2, user.getUserId());
		Object obj = query.getSingleResult();
		double result = 0;
		if (obj != null) {
			result = Double.parseDouble(obj + "");
		}
		return result;
	}
	@Override
	public PageDataList<UserVipGrowthLogModel> getUserVipGrowthLogListByUserId(
			long userId, int page, int size) {
		QueryParam param = QueryParam.getInstance();
		if(userId>0){
			param.addParam("user.userId", userId);
		}
		param.addOrder(OrderType.DESC, "addTime").addOrder(OrderType.DESC, "growthTotal");
		if (size != 0) {
	        param.addPage(page, size);
		} else {
		    param.addPage(page);
		}
		PageDataList<UserVipGrowthLog> pageDataList = findPageList(param);
		PageDataList<UserVipGrowthLogModel> pageDataList_ = new PageDataList<UserVipGrowthLogModel>();
		List<UserVipGrowthLogModel> list = new ArrayList<UserVipGrowthLogModel>();
		pageDataList_.setPage(pageDataList.getPage());
		if (pageDataList.getList().size() > 0) {
			for (int i = 0; i < pageDataList.getList().size(); i++) {
				UserVipGrowthLog userVipGrowthLog = (UserVipGrowthLog) pageDataList.getList().get(i);
				UserVipGrowthLogModel model = UserVipGrowthLogModel.instance(userVipGrowthLog);
				String username = userVipGrowthLog.getUser().getUserName();
				model.setUserName(username);
				list.add(model);
			}
		}
		pageDataList_.setList(list);
		return pageDataList_;
	}
	
}
